# -*- coding: utf-8 -*-
# @Time   : 2022-02-02 13:34:04
# @Author : 搬砖客

import random
from typing import Text
import datetime
from utils.logging_utils.log_util import write_error_log, write_warning_log, write_log
from utils.process_files_utils.yaml_util import YamlUtil
from utils.mysql_utils.database_util import Database


class DebugTalk:

    def __init__(self):
        pass

    # 获得随机整数的方法
    @staticmethod
    def get_random_number(min_number: int, max_number: int) -> int:
        """
        :param min_number: 随机数开始位的值
        :param max_number: 随机数结束位的值
        :return: 返回随机数，int型
        """
        return random.randint(int(min_number), int(max_number))

    @staticmethod
    def get_now_time(fmt: Text) -> Text:
        """
        :param fmt: 返回时间的格式，比如："%Y-%m-%d %H:%M:%S""
        :return:
        """
        now_time = datetime.datetime.now()
        return str(now_time.strftime(fmt))

    # 获取extract.yaml文件的值
    @staticmethod
    def get_extract_data(key: Text):
        """
        :param key:读取数据的key值
        :return: 将key对应的value值写入extract.yaml文件
        """
        try:
            return YamlUtil.read_extract_yaml(key)
        except Exception as e:
            write_error_log("读取extract文件失败，请确认文件中是否有'{}'关键字，错误信息为：{}".format(key, e))

    # 讲一个参数放入全局文件中
    @staticmethod
    def extract_msg(key: Text, value: Text):
        """
        :return: 将{key,value} 写入extract.yaml文件
        """
        YamlUtil.write_extract_yaml({key: value})

    # 获取基础路径
    @staticmethod
    def get_base_url(node: Text):
        """
        :return: common.yaml文件中url下面的其他url地址
        """
        return YamlUtil.read_config_yaml("url", node)

    # 获取config中api_info下面的值
    @staticmethod
    def get_api_info(node):
        """
        :return: common.yaml文件中api_info下面的值
        """
        return YamlUtil.read_config_yaml("api_info", node)

    @staticmethod
    def extract_sql(sql_statement: str, key: str):
        """
        读取数据库的值，将sql_statement的值座位value，key为key存入extract.yaml中
        :param sql_statement: sql语句返回的值
        :param key: 需存入extract.yaml的key
        :return:
        """
        database = Database()
        database_values = database.query_one_dict(sql_statement)
        if database_values is not None:
            for value in database_values.values():
                YamlUtil.write_extract_yaml({key: value})
                write_log(f"进行sql内容提取键值对，提取成功，key为：【{key}】，value为：【{value}】")
        else:
            raise Exception(f"通过sql语句提取键值对失败，sql返回的数据为：{database_values}，对应的key为：{key}")


if __name__ == '__main__':
    # sql = "SELECT id FROM `imotor`.`m_report_rule` WHERE rule_name like '%接口自动化测试%'ORDER BY id DESC LIMIT 1 "
    # sql = "SELECT id FROM `imotor`.`m_report_rule` WHERE rule_name like '\u63a5\u53e3\u81ea\u52a8\u5316\u6d4b\u8bd5'ORDER BY id DESC LIMIT 1 "
    # res = DebugTalk.extract_sql(sql, "这个是建")
    sql2 = "SELECT id FROM `imotor`.`m_report_rule` WHERE rule_name like '%\u63a5\u53e3\u81ea\u52a8\u5316\u6d4b\u8bd5%'ORDER BY id DESC LIMIT 1"
    print(sql2)
